<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="../css/font-awesome/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="../css/ionicons/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="../css/AdminLTE/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect. -->
    <link rel="stylesheet" href="../css/skins/skin-blue.min.css">
    <link rel="stylesheet" href="../css/fileinput/fileinput.min.css">

    <link rel="stylesheet" href="../css/bootstrap/bootstrap-editable.css">
    <link rel="stylesheet" href="../css/bootstrap/bootstrap-table.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="../js/plugins/browserhacks/html5shiv.min.js"></script>
    <script src="../js/plugins/browserhacks/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery 3 -->
    <script src="../js/plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap 3.3.7 -->
    <script src="../js/plugins/bootstrap/bootstrap.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../js/plugins/adminlte/adminlte.min.js"></script>

    <script src="../js/plugins/bootstrap/tableExport.min.js"></script>

    <script src="../js/plugins/bootstrap/bootstrap3-editable.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-export.min.js"></script>
    <script src="../js/plugins/bootstrap/bootstrap-table-editable.min.js"></script>

    <!-- 文件上传插件-->
    <script src="../js/plugins/fileinput/fileinput.min.js"></script>
    <script src="../js/plugins/fileinput/zh.js"></script>
    <script src="../js/plugins/fileinput/theme.js"></script>

</head>
<body ng-app="myApp" ng-controller="filemyController">
<div class="container-fluid">
    <form class="form-horizontal">
        <div class="form-group" style="margin-top:20px">
            <label for="location" class="col-sm-2 control-label">选择文件</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id='location' class="form-control" onclick="$('#i-file').click();">
                    <label class="input-group-btn">
                        <input type="button" id="i-check" value="浏览文件" class="btn btn-primary"
                               onclick="$('#i-file').click();">
                    </label>
                </div>
            </div>
            <input type="file" name="file" id='i-file'  accept=".xls, .xlsx" onchange="angular.element(this).scope().readMyFile(this);" style="display: none">
        </div>
     </form>
</div>
<div class="container">

    <div class="row">
        <div class="col-sm-12">
            <div id="toolbar">

            </div>
            <table
                    id="table"
                    data-search="true"
                    data-toggle="table"
                    data-toolbar="#toolbar"
                    data-show-columns="true"
                    data-click-to-select="true"
                    data-show-pagination-switch="true"
                    data-pagination="true"
                    data-id-field="id"
                    data-page-list="[10, 25, 50, 100, all]"
                    data-show-footer="true"
                    data-show-export="true">
                <thead>
                <tr>
                    <th data-field="学号" data-sortable="true">学号</th>
                    <th data-field="姓名" data-editable="true">姓名</th>
                    <th data-field="性别">性别</th>
                    <th data-field="学院">学院</th>
                    <th data-field="录取专业名称">录取专业名称</th>
                    <th data-field="专业名称（标准）">专业名称（标准）</th>
                    <th data-field="班名">班名</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-2 col-sm-offset-8">
            <button class="btn btn-block btn-primary" ng-click="addDatas();">新增</button>

        </div>
        <div class="col-sm-2">
            <button class="btn btn-block btn-primary" ng-click="saveDatas();">保存</button>
        </div>
    </div>
</div>
<script src="../js/plugins/angularjs/angular.min.js"></script>
<script src="../js/plugins/xlsx/xlsx.core.min.js"></script>
<script>
    function responseHandler(res) {
        console.log('responseHandler',res);
        res.forEach(function (row, i) {
            row.index = i+1;
        });
        return res
    }
</script>
<script>
    var app = angular.module("myApp", []);

    /*app.directive('importSheet', function () {
        return {
            scope:true,
            link: function (scope, element, attr) {
                /!**
                 * exp:<input type="file" name="uploadExcel" data-sheetname="批量导入" import-sheet multiple="false" />
                 *!/
                    //表格所在sheet页名称
               // var sheetName = attr.sheetname;
                scope.dataList = [];
                element.on('change', function (changeEvent) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var bstr = e.target.result;

                        var workbook = XLSX.read(bstr, {type: 'binary'});
                        console.log('workbook', workbook);
                        //将原始excel数据解析成json
                       // var datas = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
                        var datas=[];
                        console.log('dataList', datas);

                        for (var sheet in workbook.Sheets) {
                            if (workbook.Sheets.hasOwnProperty(sheet)) {
                                fromTo = workbook.Sheets[sheet]['!ref'];
                                datas = datas.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                            }
                        }

                        console.log('datas', datas);
                        scope.dataList = datas;

                    };
                    reader.readAsBinaryString(changeEvent.target.files[0]);
                })
            }
        }
    });*/

    app.controller("filemyController", function ($scope) {

        $scope.saveDatas=function(){
            var $table = $('#table');
            alert(JSON.stringify($table.bootstrapTable('getData')))
        }

        $scope.readMyFile=function(obj){
            $('#location').val($('#i-file').val());
            var files = obj.files;

            var fileReader = new FileReader();
            console.log("$scope--1",$scope);
            fileReader.onload = function(ev) {
                try {
                    var data = ev.target.result,
                        workbook = XLSX.read(data, {
                            type: 'binary'
                        }), // 以二进制流方式读取得到整份excel表格对象
                        persons = []; // 存储获取到的数据
                } catch (e) {
                    console.log('文件类型不正确');
                    return;
                }

                // 表格的表格范围，可用于判断表头是否数量是否正确
                var fromTo = '';
                // 遍历每张表读取
                for (var sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        fromTo = workbook.Sheets[sheet]['!ref'];
                        console.log(fromTo);
                        persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                        // break; // 如果只取第一张表，就取消注释这行
                    }
                }
                $scope.datas=persons;
                $scope.$apply();//刷新数据
                console.log("$scope--2",$scope);

                /*****表格样式*****/
                var $table = $('#table')
                var $button = $('#button')

                /*function randomData() {
                    var startId = ~~(Math.random() * 100)
                    var rows = []

                    for (var i = 0; i < 10; i++) {
                        rows.push({
                            id: startId + i,
                            name: 'test' + (startId + i),
                            price: '$' + (startId + i)
                        })
                    }
                    return rows
                }
*/
               /* var tableDatas={};
                tableDatas.total=$scope.datas.length;
                tableDatas.rows=$scope.datas;
                tableDatas.totalNotFiltered=100;*/


                $table.bootstrapTable('load',$scope.datas );
             };
            /*****表格样式*****/

            // 以二进制方式打开文件
            fileReader.readAsBinaryString(obj.files[0]);

        }
     });
</script>


</body>
</html>